<template>
  <el-dialog :title="title" :visible.sync="showFlag" width="1200px" id="processSelect" center append-to-body>
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="工序编码" prop="processCode">
        <el-input
          v-model="queryParams.processCode"
          placeholder="请输入工序编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="工序名称" prop="processName">
        <el-input
          v-model="queryParams.processName"
          placeholder="请输入工序名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-table border v-loading="loading" :data="templateprocessList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="工序编码" align="center" prop="processCode" >
      </el-table-column>
      <el-table-column label="工序名称" align="center" prop="processName" :show-overflow-tooltip="true"/>
      <el-table-column label="计价方式" align="center" prop="pricingMethod" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.pro_pricing_method" :value="scope.row.pricingMethod"/>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" :show-overflow-tooltip="true"/>
    </el-table>

  <div slot="footer" class="dialog-footer">
    <el-button type="primary" @click="submitForm">确 定</el-button>
    <el-button @click="cancel">取 消</el-button>
  </div>
  </el-dialog>
</template>

<script>
import { listAllProcess } from "@/api/mes/pro/process";
export default {
  name: "ProcessSelect",
  dicts: ['pro_pricing_method'],
  props:{
      optType: null,
      templateId: null,
    templateprocessListFromP:{
        type: Array,
        default: () => []
    },
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 检测模板-产品表格数据
      templateprocessList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      showFlag: false,
      // 查询参数
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        processCode: null,        processName: null,      },
      // 表单参数
      form: {},
      selectedProcess: [],
      // 表单校验
      rules: {
        templateId: [
          { required: true, message: "检测模板ID不能为空", trigger: "blur" }
        ],
        itemId: [
          { required: true, message: "产品物料不能为空", trigger: "blur" }
        ],
      }
    };
  },

  watch: {
    showFlag:{
      immediate: true,
      handler(newVal, oldVal){
      if (newVal) {
        this.resetQuery();
      }
    }
  }},
  methods: {
    /** 查询检测模板-工序列表 */
    getList() {
      this.loading = true;
      listAllProcess(this.queryParams).then(response => {
        this.templateprocessList = response.data.filter(item =>
          !this.templateprocessListFromP.some(item2 => item2.processId == item.processId)
        );
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.showFlag = false;
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.processCode=''
      this.queryParams.processName=''
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.selectedProcess = selection;
    },

    /** 提交按钮 */
    submitForm() {
      if(this.selectedProcess.length == 0){
        this.$message.warning("请至少选择一个工序！");
      }else{
       this.$emit("onSelected",this.selectedProcess);
       this.showFlag=false;
      }
    },

  }
};
</script>
<style lang="scss">
#processSelect .el-dialog{
  height:80%;
  overflow-y: scroll;
  .el-dialog__footer{
    position: sticky;
    bottom: 0;
    background-color: white;
  }
}

</style>
